package exam;

import java.util.Scanner;

public class AliExam1 {

    static void calculate(String[] locations) {
        int n = locations.length;
        int[][] a = new int[n][2];
        for(int i=0;i<n;i++){
            a[i][0] = Integer.valueOf(locations[i].charAt(0));
            a[i][2] = Integer.valueOf(locations[i].charAt(3));
        }
        dfs(n,a);
    }

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine().trim());
        int index = 0;
        String[] locations = new String[num];
        while(num-- > 0){
            locations[index++] = scanner.nextLine();
        }

        calculate(locations);
    }

    static void dfs(int n,int[][] location){
        int maxX=0,maxY=0;
        for(int[] x:location){
            if(x[0] > maxX){
                maxX = x[0];
            }
            if(x[1] >maxY){
                maxY = x[1];
            }
        }
        dfs2(location,maxX,maxY,0,0,0,0,n);
    }
    static void dfs2(int[][] location,int maxX,int maxY,int count,int x,int y,int step,int n){
        int next [][] = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};

        if(count == 3){
            System.out.println(step);
            return;
        }
        for(int i = 0;i<=3;i++){
            x = x + next[i][0];
            y = y + next[i][1];
            step++;
            if(x > maxX || y > maxY){
                x = x + next[i][0];
                y = y + next[i][1];
                return;
            }
            for(int k=0;k<3;k++){
                if(location[k][0] == x && location[k][1] == y){
                    count++;
                }
            }
            dfs2(location,maxX,maxY,count,x,y,step,n);
        }
    }
}
